package com.seecode;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Toast;

import com.seecode.Database.Fuxi;
import com.seecode.Database.History;
import com.seecode.UnitClass.CodeView;
import com.seecode.Utils.Utils;

public class MainActivity extends AppCompatActivity  {
    private WebView codeWeb;
    private DrawerLayout mDraw;
    private NavigationView navigationView;
    private int screenStatus=0;
    private int FILE_SELECT_CODE=666;
    private String header=null;
    private String  tail =null;
    private CodeView mCodeView;
    private LightWebView lweb1;
    private LightWebView lweb2;
    private LightWebView lweb3;
    private LightWebView lweb4;
    private LightWebView lweb5;
    private LightWebView lweb6;
    private LightWebView lweb7;
    private LightWebView lweb8;
    private LightWebView lweb9;
    private LightWebView lweb10;



    private String  lastClassName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }

        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mDraw = (DrawerLayout) findViewById(R.id.draw);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        mCodeView = new CodeView(MainActivity.this);
       // codeWeb.loadUrl("file:///android_asset/1.html");
        init();

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                clickItem(item);
                mDraw.closeDrawers();
                return false;

            }
        });




    }
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }
    public void init() {//assets support url not file read so use resource way to get

        Utils.init(MainActivity.this);
        Fuxi.DataBaseInit(MainActivity.this);
        if (!Utils.getPreBoolValues("history")) {

            History h = new History();
            h.createTable();
        }

        lweb1 =(LightWebView)findViewById(R.id.lightwebview1);
        lweb2 =(LightWebView)findViewById(R.id.lightwebview2);
        lweb3 =(LightWebView)findViewById(R.id.lightwebview3);
        lweb4 =(LightWebView)findViewById(R.id.lightwebview4);
        lweb5 =(LightWebView)findViewById(R.id.lightwebview5);
        lweb6 =(LightWebView)findViewById(R.id.lightwebview6);
        lweb7 =(LightWebView)findViewById(R.id.lightwebview7);
        lweb8 =(LightWebView)findViewById(R.id.lightwebview8);
        lweb9 =(LightWebView)findViewById(R.id.lightwebview9);
        lweb10 =(LightWebView)findViewById(R.id.lightwebview10);
        mCodeView.addLightWebView(lweb1);
        mCodeView.addLightWebView(lweb2);
        mCodeView.addLightWebView(lweb3);
        mCodeView.addLightWebView(lweb4);
        mCodeView.addLightWebView(lweb5);
        mCodeView.addLightWebView(lweb6);
        mCodeView.addLightWebView(lweb7);
        mCodeView.addLightWebView(lweb8);
        mCodeView.addLightWebView(lweb9);
        mCodeView.addLightWebView(lweb10);
        lweb1.loadUrl("file:///android_asset/1.html");
        //this not set visi web buran will blank  so uply for you health so other setvisible is no blank
        String welcome="\n \n \n \n \n \n \n \n \n "+
                "     ---------------------\n"+
                "    /\n" +
                "   /\n"+
                "  /\n"+
                " /\n"+
                "/\n"+"   从左边开始向右滑动左侧菜单!!!\n"+
                "\\\n"+
                " \\\n"+
                "  \\\n"+
                "   \\\n"+
                "    \\\n"+
                "     ---------------------";

        Utils.showToast(welcome);
        Utils.changePathPermission("/");
        Intent intent =new Intent(this, CodeService.class);
        //startService(intent);










    }






    private void screenChange() {//tp解决屏幕旋转问题  用onconfig activity配置设置保证内容不会重来刷新

        if (screenStatus == 1) {

            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            screenStatus = 0;
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            screenStatus = 1;

        }

    }

    public void clickItem(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.nav_xz:
                    screenChange();

                break;
            case R.id.nav_file:

                showFileChooser();

                break;

            case R.id.nav_history:

                Utils.seeHistory();

                break;

            case R.id.funlist:

                    startFunList();
                break;
            case  R.id.nav_font:

                mCodeView.adjustFont();
                break;

            default:


        }

    }

    public void startFunList(){

        if(mCodeView == null){

            Toast.makeText(MainActivity.this,"还未选择文件",Toast.LENGTH_LONG).show();

            return;
        }

        Intent intent = new Intent(MainActivity.this,FunListActivity.class);
        intent.putExtra("funlist",mCodeView.getFunList());
        intent.putExtra("blocks",mCodeView.getBlocks());
        intent.putExtra("currentBlock",mCodeView.getCurrentBlock());
        startActivity(intent);
    }



    private void showFileChooser(){

        /*Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
       // intent.setType("*///*");
        /*intent.addCategory((Intent.CATEGORY_OPENABLE));
        try{
            startActivityForResult(Intent.createChooser(intent,"请选择一个要打开的文件"),FILE_SELECT_CODE);

        }catch(Exception e){

            Toast.makeText(MainActivity.this,"请安装文件管理器",Toast.LENGTH_SHORT).show();
        }*/

        Intent intent = new Intent (MainActivity.this,SearchActivity.class);
        startActivity(intent);


    }

    public  void loadCode(String file){
        Utils.saveHistory(file);

        if(mCodeView == null){
            mCodeView = new CodeView(MainActivity.this);
        }
        String retContent = Utils.readFileContent(file);
        if(retContent.equals("null") || retContent == null){

            Utils.showToast("内容不能空");
            return ;
        }
        mCodeView.showHtml(retContent);



    }


    public  void readFun(String name){

        if(mCodeView != null){

            mCodeView.showCode(name);
        }
    }






}